﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

using BaHuan.BusinessLayer;
using BaHuan.Common;
using BaHuan.Entities;

namespace BaHuan
{
    public partial class FrmCongNo : Form
    {
        private ButToanMgt buttoanMgt = new ButToanMgt();
        private KhachHangManagement khachhangMgt = new KhachHangManagement();
        private NhaCungCapManagement nhacungcapMgt = new NhaCungCapManagement();
        private ChiNhanhKhachHangManagement chinhanhkhachhangMgt = new ChiNhanhKhachHangManagement();
        private CTTKKeToanLuongTinhManagement cttkketoanluongtinhMgt = new CTTKKeToanLuongTinhManagement();

        public FrmCongNo()
        {
            InitializeComponent();
        }

        private void btnDong_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cboChonTaiKhoan_SelectedIndexChanged(object sender, EventArgs e)
        {
            cboDoiTuong.DataSource = null;
            if (cboChonTaiKhoan.SelectedIndex == 0)
            {
                cboDoiTuong.DataSource = GetAllKhachHang();
                cboDoiTuong.DisplayMember = "Ten";
                cboDoiTuong.ValueMember = "Id";
            }
            else
            {
                cboDoiTuong.DataSource = GetAllNhaCungCap();
                cboDoiTuong.DisplayMember = "TenNhaCungCap";
                cboDoiTuong.ValueMember = "Id";
            }
            cboDoiTuong.Text = "";
        }

        private void cboChonKieuXem_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cboChonKieuXem.SelectedIndex == 0)
            {
                cboDoiTuong.Enabled = false;
            }
            else
            {
                cboDoiTuong.Enabled = true;
            }

            if (cboChonKieuXem.SelectedIndex == 1)
            {
                if (cboChonTaiKhoan.SelectedIndex == 1)
                {
                    MessageBox.Show("Kiểu xem công nợ này chỉ dành cho tài khoản 131");
                    cboChonKieuXem.SelectedIndex = 0;
                }
            }
            else if (cboChonKieuXem.SelectedIndex == 2)
            {
                if (cboChonTaiKhoan.SelectedIndex == 0)
                {
                    MessageBox.Show("Kiểu xem công nợ này chỉ dành cho tài khoản 331");
                    cboChonKieuXem.SelectedIndex = 0;
                }
            }
        }

        private void btnXem_Click(object sender, EventArgs e)
        {
            DateTime dtTuNgay = dtpTuNgay.Value;
            DateTime dtDenNgay = dtpDenNgay.Value;

            string masotk = "";
            if (cboChonTaiKhoan.SelectedIndex == 0)
                masotk = "131";
            else
                masotk = "331";

            Dictionary<string, long> dictionary = new Dictionary<string, long>();
            List<CTTKKeToanLuongTinhForBCDTK> lstDoiTuong = GetAllCTTKKeToanLuongTinhForBCDTK(masotk);

            if (lstDoiTuong.Count > 0)
            {
                foreach (var p in lstDoiTuong)
                {
                    if (!dictionary.ContainsKey(p.TenKhachHang) && cboChonTaiKhoan.SelectedIndex == 0)
                        dictionary.Add(p.TenKhachHang, p.SoDuBanDau);
                    else if (!dictionary.ContainsKey(p.TenNhaCungCap) && cboChonTaiKhoan.SelectedIndex == 1)
                        dictionary.Add(p.TenNhaCungCap, p.SoDuBanDau);
                }
            }

            var lstAll = (from x in GetAllButToan()
                          where x.NgayThang.Date <= dtDenNgay.Date
                          select x);
                        
            if (cboChonKieuXem.SelectedIndex == 0)
            {
                #region Xem tong hop
                if (cboChonTaiKhoan.SelectedIndex == 0)
                {
                    #region TK 131
                    foreach (KhachHang c in GetAllKhachHang())
                    {
                        if (!dictionary.ContainsKey(c.Ten))
                        {
                            dictionary.Add(c.Ten, 0);
                        }
                    }

                    if (dictionary.Count > 0)
                    {
                        List<TongHopCongNo131> lstTongHop = new List<TongHopCongNo131>();

                        var lst131 = lstAll.Where(x => x.TKGoc.StartsWith("131") && x.NgayThang.Date < dtTuNgay.Date);
                        if (lst131.ToList().Count > 0)
                        {
                            foreach (var p in lst131)
                            {
                                if (dictionary.ContainsKey(p.MaSoKhachHang))
                                    dictionary[p.MaSoKhachHang] = (dictionary[p.MaSoKhachHang] + p.GhiNo) - p.GhiCo;
                            }
                        }
                        
                        foreach (var pairs in dictionary)
                        {
                            var lstBtw131 = lstAll.Where(x => x.TKGoc.StartsWith("131") && x.NgayThang.Date >= dtTuNgay && x.MaSoKhachHang == pairs.Key);

                            long lTempPSNo = 0;
                            long lTempPSCo = 0;
                            if (lstBtw131.ToList().Count > 0)
                            {
                                foreach (var l in lstBtw131)
                                {
                                    lTempPSNo += l.GhiNo;
                                    lTempPSCo += l.GhiCo;
                                }
                            }

                            TongHopCongNo131 tonghop131 = new TongHopCongNo131();
                            tonghop131.TenKhachHang = pairs.Key;
                            if (pairs.Value >= 0)
                                tonghop131.NoDauKy = pairs.Value;
                            else
                                tonghop131.CoDauKy = 0 - pairs.Value;
                            tonghop131.NoPhatSinh = lTempPSNo;
                            tonghop131.CoPhatSinh = lTempPSCo;

                            long lTemp = (pairs.Value + lTempPSNo) - lTempPSCo;
                            if (lTemp >= 0)
                                tonghop131.NoCuoiKy = lTemp;
                            else
                                tonghop131.CoCuoiKy = 0 - lTemp;

                            if (tonghop131.NoDauKy == 0 && tonghop131.CoDauKy == 0 && tonghop131.NoPhatSinh == 0 && tonghop131.CoPhatSinh == 0 && tonghop131.NoCuoiKy == 0 && tonghop131.CoCuoiKy == 0)
                                continue;
                            else
                                lstTongHop.Add(tonghop131);
                        }

                        if (lstTongHop.Count > 0)
                        {
                            FrmXemReport frm = new FrmXemReport(lstTongHop, dtTuNgay, dtDenNgay);
                            this.Hide();
                            frm.ShowDialog();
                            this.Show();
                        }
                        else
                            MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
                    }
                    #endregion
                }
                else
                {
                    #region TK 331
                    foreach (NhaCungCap c in GetAllNhaCungCap())
                    {
                        if (!dictionary.ContainsKey(c.TenNhaCungCap))
                        {
                            dictionary.Add(c.TenNhaCungCap, 0);
                        }
                    }

                    if (dictionary.Count > 0)
                    {
                        List<TongHopCongNo331> lstTongHop = new List<TongHopCongNo331>();

                        var lst331 = lstAll.Where(x => x.TKGoc.StartsWith("331") && x.NgayThang.Date < dtTuNgay.Date);
                        if (lst331.ToList().Count > 0)
                        {
                            foreach (var p in lst331)
                            {
                                if (dictionary.ContainsKey(p.MaSoKhachHang))
                                    dictionary[p.MaSoKhachHang] = (dictionary[p.MaSoKhachHang] + p.GhiCo) - p.GhiNo;
                            }
                        }

                        foreach (var pairs in dictionary)
                        {
                            var lstBtw331 = lstAll.Where(x => x.TKGoc.StartsWith("331") && x.NgayThang.Date >= dtTuNgay && x.MaSoKhachHang == pairs.Key);

                            long lTempPSNo = 0;
                            long lTempPSCo = 0;
                            if (lstBtw331.ToList().Count > 0)
                            {
                                foreach (var l in lstBtw331)
                                {
                                    lTempPSNo += l.GhiNo;
                                    lTempPSCo += l.GhiCo;
                                }
                            }

                            TongHopCongNo331 tonghop331 = new TongHopCongNo331();
                            tonghop331.TenKhachHang = pairs.Key;
                            if (pairs.Value >= 0)
                                tonghop331.CoDauKy = pairs.Value;
                            else
                                tonghop331.NoDauKy = 0 - pairs.Value;
                            tonghop331.NoPhatSinh = lTempPSNo;
                            tonghop331.CoPhatSinh = lTempPSCo;

                            long lTemp = (pairs.Value + lTempPSCo) - lTempPSNo;
                            if (lTemp >= 0)
                                tonghop331.CoCuoiKy = lTemp;
                            else
                                tonghop331.NoCuoiKy = 0 - lTemp;

                            if (tonghop331.NoDauKy == 0 && tonghop331.CoDauKy == 0 && tonghop331.NoPhatSinh == 0 && tonghop331.CoPhatSinh == 0 && tonghop331.NoCuoiKy == 0 && tonghop331.CoCuoiKy == 0)
                                continue;
                            else
                                lstTongHop.Add(tonghop331);
                        }

                        if (lstTongHop.Count > 0)
                        {
                            FrmXemReport frm = new FrmXemReport(lstTongHop, dtTuNgay, dtDenNgay);
                            this.Hide();
                            frm.ShowDialog();
                            this.Show();
                        }
                        else
                            MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
                    }
                    #endregion
                }
                #endregion
            }
            else if (cboChonKieuXem.SelectedIndex == 1)
            {
                #region Chi xem voi TK 131
                if (cboDoiTuong.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn đối tượng để xem báo cáo!", "Thông báo");
                    return;
                }

                List<ButToan> lst = new List<ButToan>();

                KhachHang kh = (KhachHang)cboDoiTuong.SelectedItem;

                long lDuNo = 0;
                long lDuCo = 0;

                var c = lstDoiTuong.Where(r => r.TenKhachHang == kh.Ten).FirstOrDefault();
                if (c != null)
                {
                    long lTemp = 0;

                    var lst131 = lstAll.Where(x => x.TKGoc.StartsWith("131") && x.MaSoKhachHang == c.TenKhachHang && x.NgayThang < dtTuNgay);
                    if (lst131.ToList().Count > 0)
                    {
                        foreach (var p in lst131)
                        {
                            lTemp = (lTemp + p.GhiCo) - p.GhiNo;
                        }

                        if (lTemp >= 0)
                            lDuCo = lTemp;
                        else
                            lDuNo = 0 - lTemp;
                    }                    
                }

                var lstBtw131 = (from p in GetAllButToanForCongNoPhaiThu()
                                 where p.NgayThang.Date >= dtTuNgay.Date && p.NgayThang.Date <= dtDenNgay && p.MaSoKhachHang == kh.Ten
                                 select p);

                lst = lstBtw131.ToList();

                if (lst.Count > 0)
                {
                    FrmXemReport frm = new FrmXemReport(lst, dtTuNgay, dtDenNgay, lDuNo, lDuCo, kh.Ten, 0);
                    this.Hide();
                    frm.ShowDialog();
                    this.Show();
                }
                else
                    MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
                #endregion
            }
            else if (cboChonKieuXem.SelectedIndex == 2)
            {
                #region Chi xem voi TK 331
                if (cboDoiTuong.Text == "")
                {
                    MessageBox.Show("Vui lòng chọn đối tượng để xem báo cáo!", "Thông báo");
                    return;
                }

                List<ButToan> lst = new List<ButToan>();
                NhaCungCap ncc = (NhaCungCap)cboDoiTuong.SelectedItem;

                long lDuNo = 0;
                long lDuCo = 0;

                var c = lstDoiTuong.Where(r => r.TenNhaCungCap == ncc.TenNhaCungCap).FirstOrDefault();
                if (c != null)
                {
                    long lTemp = 0;

                    var lst331 = lstAll.Where(x => x.TKGoc.StartsWith("331") && x.MaSoKhachHang == c.TenNhaCungCap && x.NgayThang < dtTuNgay);
                    if (lst331.ToList().Count > 0)
                    {                        
                        foreach (var p in lst331)
                        {
                            lTemp = (lTemp + p.GhiCo) - p.GhiNo;
                        }

                        if (lTemp >= 0)
                            lDuCo = lTemp;
                        else
                            lDuNo = 0 - lTemp;
                    }
                }

                var lstBtw331 = (from p in GetAllButToanForCongNoPhaiTra()
                                 where p.NgayThang.Date >= dtTuNgay.Date && p.NgayThang.Date <= dtDenNgay && p.MaSoKhachHang == ncc.TenNhaCungCap
                                 select p);

                lst = lstBtw331.ToList();

                if (lst.Count > 0)
                {
                    FrmXemReport frm = new FrmXemReport(lst, dtTuNgay, dtDenNgay, lDuNo, lDuCo, ncc.TenNhaCungCap, 1);
                    this.Hide();
                    frm.ShowDialog();
                    this.Show();
                }
                else
                    MessageBox.Show("Không có dữ liệu phù hợp!", "Thông báo");
                #endregion
            }
        }

        private void cboDoiTuong_Leave(object sender, EventArgs e)
        {
            if (cboChonTaiKhoan.SelectedIndex == 0)
            {
                foreach (KhachHang c in cboDoiTuong.Items)
                {
                    if (c.Ten.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                    {
                        cboDoiTuong.SelectedValue = c.Id;
                        break;
                    }
                }
            }
            else if (cboChonTaiKhoan.SelectedIndex == 1)
            {
                foreach (NhaCungCap p in cboDoiTuong.Items)
                {
                    if (p.TenNhaCungCap.ToLower().Contains(cboDoiTuong.Text) && cboDoiTuong.Text != "")
                    {
                        cboDoiTuong.SelectedValue = p.Id;
                        break;
                    }
                }
            }

            if (cboDoiTuong.SelectedValue == null)
            {
                cboDoiTuong.Text = "";
                if (cboChonKieuXem.SelectedIndex > 0)
                    cboDoiTuong.Focus();
            }
        }

        private void cboChonTaiKhoan_Leave(object sender, EventArgs e)
        {
            if (cboChonKieuXem.SelectedIndex == 1)
            {
                if (cboChonTaiKhoan.SelectedIndex == 1)
                {
                    MessageBox.Show("Kiểu xem công nợ này chỉ dành cho tài khoản 131");
                    cboChonKieuXem.SelectedIndex = 0;
                }
            }
            else if (cboChonKieuXem.SelectedIndex == 2)
            {
                if (cboChonTaiKhoan.SelectedIndex == 0)
                {
                    MessageBox.Show("Kiểu xem công nợ này chỉ dành cho tài khoản 331");
                    cboChonKieuXem.SelectedIndex = 0;
                }
            }
        }        

        public List<ButToan> GetAllButToan()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToan();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<KhachHang> GetAllKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = khachhangMgt.GetAllKhachHang();
            List<KhachHang> listKhachHang = (List<KhachHang>)resultObj.Result;

            return listKhachHang;
        }

        public List<NhaCungCap> GetAllNhaCungCap()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = nhacungcapMgt.GetAllNhaCungCap();
            List<NhaCungCap> listNhaCungCap = (List<NhaCungCap>)resultObj.Result;

            return listNhaCungCap;
        }

        public List<ButToan> GetAllButToanForCongNoPhaiTra()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToanForCongNoPhaiTra();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ButToan> GetAllButToanForCongNoPhaiThu()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = buttoanMgt.GetAllButToanForCongNoPhaiThu();
            List<ButToan> listButToan = (List<ButToan>)resultObj.Result;

            return listButToan;
        }

        public List<ChiNhanhKhachHang> GetAllChiNhanhKhachHang()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = chinhanhkhachhangMgt.GetAllChiNhanhKhachHang();
            List<ChiNhanhKhachHang> lstchinhanhkhachhang = (List<ChiNhanhKhachHang>)resultObj.Result;

            return lstchinhanhkhachhang;
        }

        public List<CTTKKeToanLuongTinh> GetAllCTTKKeToanLuongTinh()
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinh();
            List<CTTKKeToanLuongTinh> listCTTKKeToanLuongTinh = (List<CTTKKeToanLuongTinh>)resultObj.Result;

            return listCTTKKeToanLuongTinh;
        }

        public List<CTTKKeToanLuongTinhForBCDTK> GetAllCTTKKeToanLuongTinhForBCDTK(string MaSoTK)
        {
            ResultObj resultObj = new ResultObj();

            resultObj = cttkketoanluongtinhMgt.GetAllCTTKKeToanLuongTinhForBCDTK(MaSoTK);
            List<CTTKKeToanLuongTinhForBCDTK> listCTTKKeToanLuongTinhForBCDTK = (List<CTTKKeToanLuongTinhForBCDTK>)resultObj.Result;

            return listCTTKKeToanLuongTinhForBCDTK;
        }
    }
}
